package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Order;
import com.mall.db.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzz
 * @since 2021-09-27
 */
@Controller
@RequestMapping("/order/oc")
public class OrderController  extends BaseController<Order> {

    @Resource(name = "orderService")
    private OrderService os;

    @Override
    public Map<String,Object> find(Order order){


        PageHelper.startPage(order.getPage(),order.getLimit());

        QueryWrapper<Order> qw = new QueryWrapper<>();

        List<Order> list = os.list(qw);

        PageInfo<Order> page = new PageInfo<>(list,order.getLimit());

        return this.findMap(page.getList(), page.getTotal());
    }


    @Override
    public Map<String, Object> add(Order order) {
        order.setCreateTime(new Date());
        boolean k = os.save(order);
        return this.iud("新增",k,order);
    }

    @Override
    public Map<String, Object> update(Order order) {
        boolean b = os.updateById(order);
        return this.iud("编辑", b, order);
    }

    @Override
    public Map<String, Object> delete(Order order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order.getId())) {
            os.removeById(order.getId());
            map.put("is", true);
            map.put("msg", "删除成功!");
        } else {
            map.put("is", false);
            map.put("msg", "删除失败!");
        }
        return map;
    }

}

